package com.nkzy.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//SpringMVC的配置类代替其配置文件
@Configuration
@EnableWebMvc  //打开与Web开发相关的注解，如：ResponseBody\@RequestBody等
@ComponentScan({"com.nkzy.controller","com.nkzy.common"})
public class SpringMVCConfig implements WebMvcConfigurer {
    @Value("${front.url}")
    private String frontUrl;
    //文件上传的处理器
    @Bean
    public MultipartResolver multipartResolver(){
        return new StandardServletMultipartResolver();
    }
    //跨域访问:浏览器端访问，IP地址或端口不一样
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/users/**").allowedOrigins(frontUrl).allowCredentials(true);
    }
}

